package com.cjh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.cjh.mapper.UserMapper;
import com.cjh.pojo.Menu;
import com.cjh.pojo.User;
import com.cjh.service.UserService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index() {
        return "user-8040的端口号是：" + port;
    }

    @GetMapping("/userList")
    public PageInfo<User> userList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        PageHelper.startPage(page, limit);
        List<User> users = userService.list();
        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }

    @PostMapping("/insertUser")
    public boolean insertMenu(@RequestBody User user){
        user.setRegisterdate(new Date());
        return userService.save(user);
    }

    @DeleteMapping("/deleteUserById/{id}")
    public boolean deleteUserById(@PathVariable("id") Integer id){
        return userService.removeById(id);
    }

    @GetMapping("/selectUserById/{id}")
    public User selectUserById(@PathVariable("id") Integer id) {
        return userService.getById(id);
    }

    @PutMapping("/updateUserById")
    public boolean updateUserById(@RequestBody User user) {
        return userService.updateById(user);
    }
}
